package com.ltcode.sys.infrastructure.repository.impl;

import com.ltcode.sys.domain.model.SysUser;
import com.ltcode.sys.domain.repository.SysUserRepository;
import com.ltcode.sys.infrastructure.persistence.converter.SysUserConverter;
import com.ltcode.sys.infrastructure.persistence.entity.SysUserPO;
import com.ltcode.sys.infrastructure.persistence.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户仓储实现
 * 
 * @author ltcode
 * @since 2024-01-01
 */
@Repository
@AllArgsConstructor
public class SysUserRepositoryImpl implements SysUserRepository {

    private final SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> selectUserList(SysUser sysUser) {
        SysUserPO userPO = SysUserConverter.toPO(sysUser);
        List<SysUserPO> poList = sysUserMapper.selectUserList(userPO);
        return SysUserConverter.toDomainList(poList);
    }

    @Override
    public List<SysUser> selectAllocatedList(SysUser sysUser) {
        SysUserPO userPO = SysUserConverter.toPO(sysUser);
        List<SysUserPO> poList = sysUserMapper.selectAllocatedList(userPO);
        return SysUserConverter.toDomainList(poList);
    }

    @Override
    public List<SysUser> selectUnallocatedList(SysUser sysUser) {
        SysUserPO userPO = SysUserConverter.toPO(sysUser);
        List<SysUserPO> poList = sysUserMapper.selectUnallocatedList(userPO);
        return SysUserConverter.toDomainList(poList);
    }

    @Override
    public SysUser selectUserByUserName(String userName) {
        SysUserPO po = sysUserMapper.selectUserByUserName(userName);
        return SysUserConverter.toDomain(po);
    }

    @Override
    public SysUser selectUserById(Long userId) {
        SysUserPO po = sysUserMapper.selectUserById(userId);
        return SysUserConverter.toDomain(po);
    }

    @Override
    public int insertUser(SysUser user) {
        SysUserPO po = SysUserConverter.toPO(user);
        return sysUserMapper.insertUser(po);
    }

    @Override
    public int updateUser(SysUser user) {
        SysUserPO po = SysUserConverter.toPO(user);
        return sysUserMapper.updateUser(po);
    }

    @Override
    public int updateUserAvatar(String userName, String avatar) {
        return sysUserMapper.updateUserAvatar(userName, avatar);
    }

    @Override
    public int resetUserPwd(String userName, String password) {
        return sysUserMapper.resetUserPwd(userName, password);
    }

    @Override
    public int deleteUserById(Long userId) {
        return sysUserMapper.deleteUserById(userId);
    }

    @Override
    public int deleteUserByIds(Long[] userIds) {
        return sysUserMapper.deleteUserByIds(userIds);
    }

    @Override
    public boolean checkUserNameUnique(SysUser user) {
        Long userId = user.getId() == null ? -1L : user.getId();
        SysUserPO info = sysUserMapper.checkUserNameUnique(user.getUserName());
        if (info != null && info.getId().longValue() != userId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkPhoneUnique(SysUser user) {
        Long userId = user.getId() == null ? -1L : user.getId();
        SysUserPO info = sysUserMapper.checkPhoneUnique(user.getPhoneNumber());
        if (info != null && info.getId().longValue() != userId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkEmailUnique(SysUser user) {
        Long userId = user.getId() == null ? -1L : user.getId();
        SysUserPO info = sysUserMapper.checkEmailUnique(user.getEmail());
        if (info != null && info.getId().longValue() != userId.longValue()) {
            return false;
        }
        return true;
    }
}
